package com.example.zrdemo1.service.impl;

import com.example.zrdemo1.mapper.UserMapper;
import com.example.zrdemo1.mapper.UserRelMapper;
import com.example.zrdemo1.pojo.User;
import com.example.zrdemo1.pojo.UserRel;
import com.example.zrdemo1.service.ex.NullSexException;
import com.example.zrdemo1.service.ex.NullTokenException;
import com.example.zrdemo1.service.ex.NullUidException;
import com.example.zrdemo1.service.ex.UpdateException;
import com.example.zrdemo1.utils.IpInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;

@Service
public class UserService implements com.example.zrdemo1.service.UserService {

    @Autowired
    private UserMapper userMapper;

    @Resource
    private UserRelMapper userRelMapper;


    public String insertUser(User user){
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        int rows =  userMapper.insertUser(user);
        if (rows == 1){
            return "已经成功放入月老池了哦，搓搓手准备等待小红点吧！";
        }else{
            return "插入时出现未知异常";
        }

    }

    @Override
    public User selectOwnInfo(String token) throws RuntimeException{

        User user = null;
        //token为空
        if (token == null){
            throw new NullTokenException("Token为空，无法进行查询操作，请授权登录");
        }else {
            user = userMapper.getUserInfo(token);
        }

        return user;
    }

    @Override
    public void updateUserInfo (User user) throws RuntimeException{

        if (user.getUid() == null){
            throw new NullUidException("更新的Uid为空,无法进行更新操作，请重试");
        }
        int rows = userMapper.updateUser(user);
        if (rows != 1){
            throw new UpdateException("数据库更新时发生错误，请联系后台管理员");
        }
    }

    @Override
    public User selectOtherUserInfo(String token) throws RuntimeException{

        if (token == null){
            throw new NullTokenException("Token为空，无法进行查询操作，请授权登录");
        }
        String s = userMapper.getUserInfo(token).getSex();
        if (s == null){
            throw new NullSexException("获取其他用户信息时需要获取到自己的性别哦！");
        }

        List<User> users = userMapper.getOtherUser(token, s);
        //获取返回的异性用户列表的数量
        int size = users.size();
        //随机获取用户列表数量范围内的随机数
        int i = new Random().nextInt(size - 1);
        //随机返回一个异性用户
        return users.get(i);

    }

    @Override
    public User selectOtherUserInfoTwo(String sex) {

        if (sex == null){
            throw new NullSexException("获取其他用户信息时需要获取到自己的性别哦！");
        }


        List<User> userList = userMapper.getOtherUserTwo(sex);
        //获取返回的异性用户列表的数量
        int size = userList.size();
        //随机获取用户列表数量范围内的随机数
        int i = new Random().nextInt(size);
        //随机返回一个异性用户
        return userList.get(i);
    }

    @Override
    public  Integer findUser(User user) {

      return  userMapper.findUser(user).size();
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public  List<User> findAll(User user) {

        return  userMapper.findUser(user);
    }
//    active 为id
    @Override
    public User findRandomLove(String sex, Integer active, HttpServletRequest httpServletRequest) {
        User user=userMapper.findRandomLove(sex);
        user.setAddNum(user.getAddNum()+1);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
        UserRel userRel=new UserRel();
        userRel.setActiveId(active);
        userRel.setUnactiveId(user.getUid());
        userRel.setAddr(IpInfo.getAdress(httpServletRequest));

//        TimeZone.setDefault(TimeZone.getTimeZone("CST"));
//        System.out.println(new Date());
//        设置时区

        userRel.setCreateTime(LocalDateTime.now());

        userRelMapper.insert(userRel);
        return user;
    }
    public Integer del(Integer id){
        userMapper.delByid(id);
        return  1;
    }


}
